﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 用于计算特定面的UV纹理坐标
    /// </summary>
    public class UVHelper : ObjectBase
    {
        #region API 函数
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="uvhelper"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUUVHelperRelease(ref IntPtr uvhelper);

        /// <summary>
        /// 获取正面UVQ
        /// </summary>
        /// <param name="uvhelper"></param>
        /// <param name="point"></param>
        /// <param name="uvq"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUUVHelperGetFrontUVQ(IntPtr uvhelper, Point3D point, ref UVQ uvq);

        /// <summary>
        /// 获取背面UVQ
        /// </summary>
        /// <param name="uvhelper"></param>
        /// <param name="point"></param>
        /// <param name="uvq"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUUVHelperGetBackUVQ(IntPtr uvhelper, Point3D point, ref UVQ uvq);
        #endregion

        internal UVHelper(IntPtr id) => Id = id;

        /// <summary>
        /// 获取正面某个点的UVQ
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public UVQ GetFrontUVQ(Point3D point)
        {
            UVQ u = new UVQ();
            SketchUpExceptionHelper.ThrowSUResult(SUUVHelperGetFrontUVQ(Id, point, ref u));
            return u;
        }

        /// <summary>
        /// 获取背面某个点的UVQ
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public UVQ GetBackUVQ(Point3D point)
        {
            UVQ u = new UVQ();
            SketchUpExceptionHelper.ThrowSUResult(SUUVHelperGetBackUVQ(Id, point, ref u));
            return u;
        }

        public override void Dispose()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUUVHelperRelease(ref Id));
        }
    }
}